package stark.app.function.compass;

import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;

import stark.app.R;
import stark.app.databinding.ActivityCompassDemoBinding;
import stark.common.basic.base.BaseNoModelActivity;
import stark.common.basic.utils.CompassUtil;

/**
 * Author：daizhongbin on 2022/8/25
 * Email：1027154338@qq.com
 * Desc:
 */
public class CompassDemoActivity extends BaseNoModelActivity<ActivityCompassDemoBinding> {

    @Override
    protected int onCreate() {
        return R.layout.activity_compass_demo;
    }

    @Override
    protected void initView() {
        CompassUtil.getInstance().start(new CompassUtil.IListener() {
            @Override
            public void onAngleChanged(int preAngle, int curAngle) {
                RotateAnimation animation = new RotateAnimation(-preAngle, -curAngle, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
                animation.setDuration(200);
                animation.setInterpolator(new LinearInterpolator());
                animation.setFillAfter(true);
                mDataBinding.ivCompass.startAnimation(animation);
                mDataBinding.tvCompassValue.setText(getAngleText(curAngle));
            }
        });
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CompassUtil.getInstance().stop();
    }

    private String getAngleText(int angle) {
        String text = "";
        if (angle >= 345 || angle <= 15) {
            text = "北" + angle + "度";
        } else if (angle > 15 && angle <= 75) {
            text = "东北" + angle + "度";
        } else if (angle > 75 && angle <= 105) {
            text = "东" + angle + "度";
        } else if (angle > 105 && angle <= 165) {
            text = "东南" + angle + "度";
        } else if (angle > 165 && angle <= 195) {
            text = "南" + angle + "度";
        } else if (angle > 195 && angle <= 255) {
            text = "西南" + angle + "度";
        } else if (angle > 255 && angle <= 285) {
            text = "西" + angle + "度";
        } else {
            text = "西北" + angle + "度";
        }
        return text;
    }
}
